package cn.hfatec.shms.web.ss.ssMonitor.action;

import cn.hfatec.shms.web.ss.ssHd.model.SsHdParams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Arrays;
import javax.annotation.Resource;


import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.web.ss.ssMonitor.model.SsMonitorParams;
import cn.hfatec.shms.web.ss.ssMonitor.model.SsMonitor;
import cn.hfatec.shms.web.ss.ssMonitor.service.SsMonitorService;
import cn.hfatec.shms.base.ShmsBaseAction;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;


/**
 * @author hfatec codegen
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Controller
@RequestMapping("/ss/ssMonitor")
public class SsMonitorAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "SsMonitorService")
    private SsMonitorService ssMonitorService;


    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public String list(SsMonitorParams ssMonitorParams, Page page, Model model) {

        Table table = ssMonitorService.querySsMonitorTable(ssMonitorParams, page);
        model.addAttribute("ssMonitorsTable", table);
        model.addAttribute("ssMonitorParams", ssMonitorParams);
        return "/ss/ssMonitor/list";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(SsMonitorParams ssMonitorParams, Page page, Model model) {
        try {
            list(ssMonitorParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/ss/ssMonitor/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到新增页
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            SsMonitor ssMonitor = new SsMonitor();
            model.addAttribute("ssMonitor", ssMonitor);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/ss/ssMonitor/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            SsMonitor ssMonitor = ssMonitorService.getSsMonitor(id);
            model.addAttribute("ssMonitor", ssMonitor);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/ss/ssMonitor/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 根据IDs删除数据
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteSsMonitor(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if (ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            ssMonitorService.deleteSsMonitorBatch(idList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 保存数据
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveSsMonitor(@Valid SsMonitor ssMonitor, BindingResult result) {

        try {
            if (result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            ssMonitorService.saveSsMonitor(ssMonitor);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据SsMonitor保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }


    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goCheck")
    @ResponseBody
    public String goCheck(String monitorStart, String monitorEnd) {
        try {

            if (monitorStart.length() == 0) {
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, "更新开始时间不能为空").toString();
            }
            if (monitorEnd.length() == 0) {
                monitorEnd = monitorStart;
            }

            List<SsMonitor> monitor = ssMonitorService.selectMonitor(monitorStart, monitorEnd);

            if (monitor != null && !monitor.isEmpty()) {
                for (SsMonitor ssMonitor : monitor) {

                    if (ssMonitor.getTypeDif().equals("1")) {
                        ssMonitorService.deleteSsMonitors(ssMonitor.getNian(), ssMonitor.getWeekNum(), ssMonitor.getCustomName(), ssMonitor.getPhone());
                        ssMonitorService.saveSsMonitor(ssMonitor);
                    } else {
                        SsMonitor SsMonitors = ssMonitorService.getSsMonitors(ssMonitor.getNian(), ssMonitor.getWeekNum(), ssMonitor.getCustomName(), ssMonitor.getPhone());
                        if (SsMonitors != null) {
                            int res = SsMonitors.getMonitorTime().compareTo(ssMonitor.getMonitorTime());
                            if (res >= 0) {
                                ssMonitorService.deleteSsMonitors(ssMonitor.getNian(), ssMonitor.getWeekNum(), ssMonitor.getCustomName(), ssMonitor.getPhone());
                            }
                            ssMonitorService.saveSsMonitor(ssMonitor);
                        } else {
                            ssMonitorService.saveSsMonitor(ssMonitor);
                        }
                    }
                }
            }


            if (monitor != null) {
                return new JsonResponse(JsonResponse.RETURN_SUCCESS, "", monitor).toString();
            } else {
                return new JsonResponse(JsonResponse.RETURN_SUCCESS, "", new SsMonitor()).toString();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }



    /**
     * 导出excel
     */
    @RequestMapping("/export")
    @ResponseBody
    public String export(SsMonitorParams ssMonitorParams,HttpServletRequest request, HttpServletResponse response) {
        try {
            ssMonitorService.exportExcel(ssMonitorParams, request, response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }
}
